<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"/>
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <title>title</title>
    <link rel="stylesheet" type="text/css" href="../css/api.css" />
    <link rel="stylesheet" type="text/css" href="../css/style.css" />
    <script type="text/javascript" src="../script/config.js"></script>

    <style>
        header{ background-color: #f16c3c; line-height: 42px; height: 42px; position: relative;padding-top:22px;}
        header b{ width: 42px; height:42px; display: inline-block; position: relative; z-index: 1; background: url() no-repeat center; background-size: 70% auto;  }
        header b.ic-back{ background-image: url(../image/back.png);background-size:25px 25px;}
        /*header b.ic-check{ background-image: url(../image/ic-check.png); float: right; }*/
        header .title{ position: absolute; left: 0; bottom: 0; right: 0;  text-align: center; font-size: 18px;color:white;}
        #foot_a{
          position:relative;
          padding-left:10%;
          padding-right:10%;
          width:80%;
          height:45px;
          bottom:0;
          left:0;
          font-size:14px;
        }
        #foot_b{
          position:relative;
          margin-left:10%;
          margin-right:10%;
          width:80%;
          height:45px;
          bottom:0;
          left:0;
          font-size:14px;
        }
        /*#foot_sure_c{
          padding-left:10%;
          padding-right:10%;
          padding-top:10px;
          padding-bottom:10px;
          background-color:#f16c3c;
          color:white;
          border-radius:10px;
        }
        #foot_sure_p{
          padding-left:12.2%;
          padding-right:12.2%;
          padding-top:10px;
          padding-bottom:10px;
          background-color:#f16c3c;
          color:white;
          border-radius:10px;

        }*/
        /*#foot_a>div{
          float:left;
          width:45%;
          /*padding-left:2.5%;*/
          /*padding-right:2.5%;*/
          /*text-align: center;
          background-color:#f16c3c;
          border-radius:10px;
          height:40px;
          line-height:40px;
          color:white;
        }*/
        .foot_sure_c{
          margin:10%;
        }
        .foot_sure,.foot_sure_cp{
          width:100%;
          text-align: center;
          background-color:#f16c3c;
          height:40px;
          border-radius:10px;
          line-height:40px;
          color:white;
        }
        /*#foot_cancle_s{
          padding-left:14.8%;
          padding-right:14.8%;
          padding-top:10px;
          padding-bottom:10px;
          background-color:#f16c3c;
          color:white;
          border-radius:10px;
          text-align: center;
        }*/
        #foot_cancle_g{
          padding-left:14.8%;
          padding-right:14.8%;
          padding-top:10px;
          padding-bottom:10px;
          background-color:#f16c3c;
          color:white;
          border-radius:10px;
        }
    </style>
</head>
<body>

    <div id="wrap" class="flex-wrap flex-vertical">
        <header class="border-b">
            <b tapmode="hover" onclick="api.closeWin();" class="ic-back"></b>
            <div class="title" id="title">清单交付</div>
            <!-- <b tapmode="hover" onclick="funActionSheet();"  class="ic-check"></b> -->
        </header>
        <div id="main" class="flex-con">

        </div>
        <div id="foot_a">
            <!-- <span class="foot_sure" id="foot_sure_c">物流公司收货</span>
            <span class="foot_cancle" id="foot_sure_p">收货人签收</span> -->
            <!-- <div class="foot_sure_c" id="foot_sure_c">物流公司收货</div>
            <div class="foot_sure_p" id="foot_sure_p">收货人签收</div> -->
            <div class="foot_sure_cp" id="foot_sure_cp">去签收</div>
        </div>
        <div id="foot_b">
            <div class="foot_sure" id="foot_cancle_s">申请退单</div>
            <!-- <span class="foot_cancle" id="foot_cancle_g">事故出险</span> -->
        </div>
    </div>
</body>
</html>
<script type="text/javascript" src="../script/jquery-3.2.1.min.js"></script>
<script type="text/javascript" src="../script/api.js"></script>
<!-- <script type="text/javascript" src="../script/up_check.js"></script> -->
<script type="text/javascript">
var leng,typenumber;
var is_true;
var aaa;
    apiready = function(){

      api.addEventListener({
                            name:'viewappear'
                        }, function(ret, err){
                           api.setScreenOrientation({
                            orientation: 'portrait_up'
                        });
                      });
      // $api.text($api.dom("#title"),api.pageParam.name);
      var scheduling_id=api.pageParam.scheduling_id;
      check_company(scheduling_id);
      $api.get(url+'app.php/getOrderCosts?scheduling_id='+scheduling_id,function(ret){
               leng=ret.result;
               typenumber=ret.type;
            },'json');

            $api.get(url+'app.php/schedule_on?scheduling_id='+scheduling_id,function(ret){
                     is_true=ret.result;
                  },'json');
       setInterval('chaxun("'+scheduling_id+'")',1000);
        api.openFrame({
            name: 'scheduling_waitsend',
            url: './scheduling_waitsend.html',
            rect: {
                x: 0,
                y: $api.dom('header').offsetHeight,
                w: api.winWidth,
                h: $api.dom('#main').offsetHeight
            },
            pageParam: {
                scheduling_id: scheduling_id,
           }
});



$api.addEvt($api.dom("#foot_sure_c"), 'click', function(){
  $api.get(url+'app.php/getOrderCosts?scheduling_id='+scheduling_id,function(ret){
           leng=ret.result;
           typenumber=ret.type;
        },'json');

        $api.get(url+'app.php/schedule_on?scheduling_id='+scheduling_id,function(ret){
                 is_true=ret.result;
              },'json');
  var name=$api.text($api.dom("#foot_sure_c"));
  var bMap = api.require('bMap');
  bMap.getLocationServices(function(ret2, err) {
  if (ret2.enable) {
  bMap.getLocation({
      accuracy: '10m',
      autoStop: true,
      filter: 1
  }, function(ret, err) {
      if (ret.status) {
        $api.post(url+'app.php/last_map',{
         body:{
              schedule_id:scheduling_id,
              longitude:ret.lon,
              latitude:ret.lat,
              }
         },function(ret){
          //  api.toast({
          //     msg: ret.desc,
          //     duration: 1000,
          //    location: 'middle'
          //  });
       },'json');

      } else {
          // alert(err.code);

      }
  });
}else{
  alert("您现在有待交付清单,司机还在运送中,请开启定位");
  var mySettingInfo = api.require('mySettingInfo');
  mySettingInfo.settingInt({
  'index': 2
  }, function(ret, err) {
  alert(JSON.stringify(ret));
  });
}
});

  if(is_true==0){
  if(leng!=0){
    api.openWin({
        name: 'select_delivery',
        url: './select_delivery.html',
        pageParam: {
            scheduling_id: scheduling_id,
            is_wuliu:1,
       }
    });
  }else{
    api.openWin({
      name:'warn',
      url:'./warn.html',
      pageParam:{
        scheduling_id: scheduling_id,
        apptype:1,
        is_wuliu:1,
      }
    });
  }
}else{
  api.toast({
          msg :'请物流公司确认',
          duration : 2000,
          location : 'middle'
  });
}
  // api.openWin({
  //     name: 'select_delivery',
  //     url: './select_delivery.html',
  //     pageParam: {
  //         scheduling_id: scheduling_id,
  //    }
  // });

});
$api.addEvt($api.dom("#foot_sure_p"), 'click', function(){
  // api.openWin({
  //     name: 'signmain',
  //     url: './signmain.html',
  //     pageParam: {
  //         scheduling_id: scheduling_id,
  //    }
  // });

  $api.get(url+'app.php/getOrderCosts?scheduling_id='+scheduling_id,function(ret){
           leng=ret.result;
        },'json');

  $api.get(url+'app.php/schedule_on?scheduling_id='+scheduling_id,function(ret){
            is_true=ret.result;
        },'json');


if(aaa!=1){
  if(is_true==0){
  if(leng!=0){
    api.openWin({
        name: 'signmain',
        url: './signmain.html',
        slidBackEnabled:false,
        pageParam: {
            scheduling_id: scheduling_id,
            is_wuliu:2,
       }
    });
  }else{
    api.openWin({
      name:'warn',
      url:'./warn.html',
      pageParam:{
        scheduling_id: scheduling_id,
        apptype:2,
        is_wuliu:2,
      }
    });
  }
}else{
  api.toast({
          msg :'请物流公司确认',
          duration : 1000,
          location : 'middle'
  });
}
}else{
  api.toast({
          msg: '商户编号已确定，请扫码接单',
          duration : 1000,
          location : 'middle'
  });
}


});
var user = $api.getStorage('user');
var lorry_id=user.lorryid;
$api.addEvt($api.dom("#foot_cancle_s"),'click',function(){
  $api.post(url+'app.php/search_back_scheduling',{
   body:{
        scheduling_id:scheduling_id,
        }
   },function(ret){
      if(ret.result==0){
                api.toast({
                   msg:ret.desc,
                   duration: 2000,
                  location: 'middle'
                });
      }else{
        api.openFrame({
            name: 'pop_up',
            url: './pop_up.html',
            rect: {
                x: 0,
                y: $api.dom('header').offsetHeight,
                w: api.winWidth,
                h: api.winHeight-$api.dom('header').offsetHeight
            },
            pageParam: {
                scheduling_id: scheduling_id,
           }
      });
      }
   });

//   api.prompt({
//     title:'申请退单',
//     msg:'请填写原因（20个字以内）',
//     buttons: ['确定', '取消']
// }, function(ret, err) {
//     var index = ret.buttonIndex;
//     var text = ret.text;
//     if(index==1){
//       if(text.length<=20){
//
//       }else{
//         api.toast({
//            msg: '退单原因不能超过20个字',
//            duration: 2000,
//           location: 'middle'
//         });
//       }
//     }
// });
//   api.confirm({
//     title: '申请退单',
//     msg: '退单上报',
//     buttons: ['确定', '取消']
// }, function(ret, err) {
//     var index = ret.buttonIndex;
//     // alert(index)
//     if(index==1){
//       $api.post(url+'app.php/change_orders_status',{
//        body:{
//             scheduling_id:scheduling_id,
//             lorryid:lorry_id,
//             }
//        },function(ret){
//          if(ret.result==0){
//            var bMap = api.require('bMap');
//            bMap.getLocation({
//                accuracy: '10m',
//                autoStop: false,
//                filter: 1
//            }, function(msg, err1) {
//                if (msg.status) {
//                  $api.post(url+'app.php/last_map',{
//                   body:{
//                        schedule_id:scheduling_id,
//                        longitude:msg.lon,
//                        latitude:msg.lat,
//                        }
//                   },function(ret1){
//                     // api.toast({
//                     //    msg: ret1.desc,
//                     //    duration: 1000,
//                     //   location: 'middle'
//                     // });
//                 },'json');
//
//                } else {
//                    // alert(err.code);
//
//                }
//            });
//            api.toast({
//               msg:ret.desc,
//               duration: 2000,
//              location: 'middle'
//            });
//           //  api.closeToWin({
//           //    name:'main'
//           //  });
//            setTimeout("api.closeToWin({name:'main'})",2000);
//          }else{
//            api.toast({
//               msg:ret.desc,
//               duration: 2000,
//              location: 'middle'
//            });
//          }
//      },'json');
//     }
//  });
});

$api.addEvt($api.dom("#foot_cancle_g"),'click',function(){
  api.confirm({
    title: '事故出险',
    msg: '事故出险上报',
    buttons: ['确定', '取消']
}, function(ret, err) {
    var index = ret.buttonIndex;
    if(index==1){
      $api.post(url+'app.php/change_orders_status2',{
       body:{
            scheduling_id:scheduling_id,
            lorryid:lorry_id,
            }
       },function(ret){
         if(ret.result==0){
           var bMap = api.require('bMap');
           bMap.getLocation({
               accuracy: '10m',
               autoStop: false,
               filter: 1
           }, function(msg, err1) {
               if (msg.status) {
                 $api.post(url+'app.php/last_map',{
                  body:{
                       schedule_id:scheduling_id,
                       longitude:msg.lon,
                       latitude:msg.lat,
                       }
                  },function(ret1){
                    api.toast({
                       msg: ret1.desc,
                       duration: 1000,
                      location: 'middle'
                    });
                },'json');

               } else {
                   // alert(err.code);

               }
           });
           api.toast({
              msg:'已经上报物流公司',
              duration: 2000,
             location: 'middle'
           });
           api.closeToWin({
             name:'main'
           });
         }else{
           api.toast({
              msg:'上报失败',
              duration: 2000,
             location: 'middle'
           });
         }

     },'json');
   }
});

});



$api.addEvt($api.dom("#foot_sure_cp"), 'click', function(){
  $api.get(url+'app.php/getOrderCosts?scheduling_id='+scheduling_id,function(ret){
           leng=ret.result;
           typenumber=ret.type;
        },'json');
        $api.get(url+'app.php/schedule_on?scheduling_id='+scheduling_id,function(ret){
                 is_true=ret.result;
              },'json');
  var name=$api.text($api.dom("#foot_sure_c"));
  var bMap = api.require('bMap');
  bMap.getLocationServices(function(ret2, err) {
  if (ret2.enable) {
  bMap.getLocation({
      accuracy: '10m',
      autoStop: true,
      filter: 1
  }, function(ret, err) {
      if (ret.status) {
        $api.post(url+'app.php/last_map',{
         body:{
              schedule_id:scheduling_id,
              longitude:ret.lon,
              latitude:ret.lat,
              }
         },function(ret){
          //  api.toast({
          //     msg: ret.desc,
          //     duration: 1000,
          //    location: 'middle'
          //  });
       },'json');

      } else {
          // alert(err.code);

      }
  });
}else{
  alert("您现在有待交付清单,司机还在运送中,请开启定位");
  var mySettingInfo = api.require('mySettingInfo');
  mySettingInfo.settingInt({
  'index': 2
  }, function(ret, err) {
  alert(JSON.stringify(ret));
  });
}
});

  if(is_true==0){
    if(typenumber==1){
      if(leng!=0){
        api.openWin({
            name: 'select_delivery',
            url: './select_delivery.html',
            pageParam: {
                scheduling_id: scheduling_id,
                is_wuliu:1,
           }
        });
      }else{
        api.openWin({
          name:'warn',
          url:'./warn.html',
          pageParam:{
            scheduling_id: scheduling_id,
            apptype:1,
            is_wuliu:1,
          }
        });
      }
    }else{
        if(leng!=0){
          api.openWin({
              name: 'signmain',
              url: './signmain.html',
              slidBackEnabled:false,
              pageParam: {
                  scheduling_id: scheduling_id,
                  is_wuliu:2,
             }
          });
        }else{
          api.openWin({
            name:'warn',
            url:'./warn.html',
            pageParam:{
              scheduling_id: scheduling_id,
              apptype:2,
              is_wuliu:2,
            }
          });
        }


}
}else{
  api.toast({
          msg :'请物流公司确认',
          duration : 2000,
          location : 'middle'
  });
}

  // api.openWin({
  //     name: 'select_delivery',
  //     url: './select_delivery.html',
  //     pageParam: {
  //         scheduling_id: scheduling_id,
  //    }
  // });

});
}
var upload_map=function(id){
  // alert(1)
  var bMap = api.require('bMap');
  bMap.getLocation({
      accuracy: '10m',
      autoStop: true,
      filter: 1
  }, function(ret, err) {
      if (ret.status) {
        $api.post(url+'app.php/scmap',{
         body:{
              lorryid:id,
              longitude:ret.lon,
              latitude:ret.lat,
              }
         },function(ret){
        //  alert(ret.desc);
       },'json');
      } else {
          // alert(err.code);
      }
  });
}
var check=function(id,time){
  $api.post(url+'app.php/check',{
        body:{lorryid:id,
              time:time,}
       },function(ret){
        if(ret.result==4){
          alert(ret.desc);
          api.openWin({
            name: 'index',
            url:  '../index.html',
          });
           setTimeout("api.closeWin({name:'main'})",650);
           $api.rmStorage('password');
           var key = 'user';
           var user = {};
           var aa=$api.getStorage(key);
           user.lorryid=aa.lorryid;
            user.telephone=aa.telephone;
           user.flag= aa.flag
           user.pass= '';
           $api.setStorage(key, user);
           api.alert({
             title: '提示消息',
             msg: ret.desc,
             buttons: ['确定']
           });
         }else{
          //  alert(ret.desc);
         }
       });
}
function chaxun(scheduling_id){
  $api.get(url+'app.php/getOrderCosts?scheduling_id='+scheduling_id,function(ret){
           leng=ret.result;
          //  alert(1)
        },'json');

        $api.get(url+'app.php/schedule_on?scheduling_id='+scheduling_id,function(ret){
                 is_true=ret.result;
              },'json');
}
function check_company(scheduling_id){
  $api.get(url+'app.php/company_customer?scheduling_id='+scheduling_id+'',function(ret){
       if(ret.result==0){
         aaa=1;
       }
     },'json');
}


</script>
